<?php
	namespace ampleharvest\email\group;
	use\ampleharvest\code\usermanager\tables;
	/**
	 * @package AmpleHarvest
	 */

	require_once dirname(dirname(dirname(dirname(__FILE__)))) . '/config/cConfig.php';
	$codeConfig = new \ampleharvest\code\cConfig();
	
	require_once dirname(dirname(__FILE__)) . "/cMailNotification.php";
	require_once dirname(dirname(dirname(__FILE__))) . "/User_Manager/Tables/cTABLE_Groups.php";
	require_once dirname(dirname(dirname(__FILE__))) . "/Tables/cTABLE_UserGroups.php";
	require_once dirname(dirname(dirname(__FILE__))) . "/classification/cMarkings.php";
	require_once $codeConfig->C_STR_USER_MANAGER_PATH . "/Classes/cTABLE_Users.php";
	require_once $codeConfig->C_STR_USER_MANAGER_PATH . "/Classes/cTABLE_Users2Groups.php";

	/**
	 * @brief Class Extends the cMailNotification Class and is used to send notifictions about Queries
	 *
	 * - Revision History
	 *  - 0.1
	 * 	 - <b>Date</b>: 11 NOV 2011
	 * 	 - <b>Author</b>: Randy Blancett
	 * 	  - Initial Version
	 *
	 * @author Randy Blancett
	 * @version 0.1
	 * @ingroup code classes
	 */
	class cRequestNotification extends \cMailNotification
	{
		protected $m_str_GroupID = null;

		protected $m_obj_TABLE_Users = null;
		protected $m_obj_TABLE_Users2Groups = null;
		protected $m_obj_TABLE_User_Groups = null;

		public function __construct()
		{
			$this->m_obj_TABLE_Users = new \cTABLE_Users();
			$this->m_obj_TABLE_Users2Groups = new \cTABLE_Users2Groups();
			$this->m_obj_TABLE_User_Groups = new \cTABLE_UserGroups();
		}

		/**
		 * Sets the ID of the harvest Request
		 * @param string $str_RequestID ID of the Request the notification is about (GUID)
		 */
		public function set_GroupID($str_ID)
		{
			$this->m_str_GroupID = $str_ID;
		}

		public function send()
		{
			if (!$this->m_obj_TABLE_User_Groups->load_DataFromID($this->m_str_GroupID))
			{
				$this->set_Error($this->m_str_GroupID . "is an unknown ID.");
				return false;
			}

			foreach ($this->m_obj_TABLE_Users2Groups->get_MembersAsArray(tables\cTABLE_Groups::C_STR_GROUP_AH_GROUP_ADMIN) as $str_UserID)
			{
				if ($this->m_obj_TABLE_Users->load_DataFromUserID($str_UserID))
				{
					$str_Email = $this->m_obj_TABLE_Users->get_Email1();
					if ($str_Email)
					{
						$this->add_To($this->m_obj_TABLE_Users->get_Email1());
					}
				}
			}

			$this->m_obj_TABLE_Users->load_DataFromUserID($this->m_obj_TABLE_User_Groups->get_requester());
			$str_Email = $this->m_obj_TABLE_Users->get_Email1();
			if ($str_Email)
			{
				$this->add_CC($str_Email);
			}

			$this->set_Subject("New Group Request");

			//	$int_Errors = ini_get("display_errors");
			//	ini_set('display_errors', 0);
			$bool_Return = parent::send();

			//	ini_set('display_errors', $int_Errors);

			return $bool_Return;
		}

		public function get_Body()
		{
			$str_Output = "";

			$obj_Markings = new \cMarkings($this->m_obj_TABLE_User_Groups->get_classification(),
				$this->m_obj_TABLE_User_Groups->get_sci(),
				$this->m_obj_TABLE_User_Groups->get_distro());
				
			$str_UserName = $this->m_obj_TABLE_User_Groups->get_requester();
				
			if ($this->m_obj_TABLE_Users->load_DataFromUserID($str_UserName))
				{
					$str_UserName = $this->m_obj_TABLE_Users->get_LastName();
					$str_UserName .= ", ".$this->m_obj_TABLE_Users->get_FirstName();
					$str_UserName .= " (".$this->m_obj_TABLE_Users->get_UserName().")";
				}

			$str_Output .= $obj_Markings->get_OverallEmailBanner();

			$str_Output .= $str_UserName." Requested that the following group be created.\n";
			
			$str_Output .= "Group Name: ".$this->m_obj_TABLE_User_Groups->get_name()."\n";
			$str_Output .= "Group Description: ".$this->m_obj_TABLE_User_Groups->get_description()."\n";
			$str_Output .= "Group Justification: ".$this->m_obj_TABLE_User_Groups->get_justification()."\n";

			$str_Output .= $obj_Markings->get_OverallEmailBanner();

			return $str_Output;
		}

	}
?>